package com.caigou.controller;

import com.caigou.annotation.OperLog;
import com.caigou.bean.cms.entity.SystemMessage;
import com.caigou.bean.cms.param.SystemMessageParam;
import com.caigou.bean.cms.param.SystemMessageQueryParam;
import com.caigou.bean.exam.entity.Users;
import com.caigou.constant.NewsConstant;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.SystemMessageService;
import com.caigou.util.Result;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/systemMessage/")
public class SystemMessageController {

    @Resource
    private SystemMessageService systemMessageService;

    @RequestMapping("getMessageId")
    @PreAuthorize("hasAnyAuthority('chakanxiaoxi')")
    public Result getMessageId(){
        Long messageId = systemMessageService.getMessageId();
        SystemMessage systemMessage = new SystemMessage();
        systemMessage.setMessageId(messageId);
        systemMessage.setStrMessageId(String.valueOf(messageId));
        return Result.success(systemMessage);
    }

    @RequestMapping("saveSystemMessage")
    @OperLog(operModule = NewsConstant.SYSTEM_MESSAGE_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "创建系统消息")
    @PreAuthorize("hasAnyAuthority('xiaoxiguanli')")
    public Result saveSystemMessage(@RequestBody SystemMessageParam systemMessageParam, @CurrentUser Users user){
        Long messageId = systemMessageParam.getMessageId();
        log.info("messageId={}", messageId);
        return systemMessageService.saveSystemMessage(systemMessageParam, user);
    }

    @RequestMapping("updateSystemMessage")
    @OperLog(operModule = NewsConstant.SYSTEM_MESSAGE_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改系统消息")
    @PreAuthorize("hasAnyAuthority('xiaoxiguanli')")
    public Result updateSystemMessage(@RequestBody SystemMessageParam systemMessageParam, @CurrentUser Users user){
        return systemMessageService.updateSystemMessage(systemMessageParam, user);
    }

    @RequestMapping("querySystemMessage")
    @PreAuthorize("hasAnyAuthority('chakanxiaoxi')")
    public Result querySystemMessage(@RequestBody SystemMessageQueryParam systemMessageQueryParam, @CurrentUser Users user){
        PageInfo<SystemMessage> pageInfoSystemMessage = systemMessageService.querySystemMessage(systemMessageQueryParam);
        return Result.success(pageInfoSystemMessage);
    }

    @RequestMapping("getSystemMessage")
    @PreAuthorize("hasAnyAuthority('chakanxiaoxi')")
    public Result getSystemMessage(@RequestBody SystemMessageParam systemMessageParam, @CurrentUser Users user){
        SystemMessage systemMessage = systemMessageService.selectByPrimaryKey(systemMessageParam.getId());
        return Result.success(systemMessage);
    }

    @RequestMapping("deleteSystemMessage")
    @OperLog(operModule = NewsConstant.SYSTEM_MESSAGE_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除系统消息")
    @PreAuthorize("hasAnyAuthority('xiaoxiguanli')")
    public Result deleteSystemMessage(@RequestBody SystemMessageParam systemMessageParam, @CurrentUser Users user){
        int count = systemMessageService.deleteByPrimaryKey(systemMessageParam.getId());
        if(count>0){
            return Result.success("删除系统消息成功");
        }else{
            return Result.error("删除系统消息失败");
        }
   }

    @RequestMapping("withdrawSystemMessage")
    @OperLog(operModule = NewsConstant.SYSTEM_MESSAGE_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "撤回系统消息")
    @PreAuthorize("hasAnyAuthority('xiaoxiguanli')")
    public Result withdrawSystemMessage(@RequestBody SystemMessageParam systemMessageParam, @CurrentUser Users user){
        int count = systemMessageService.withdrawSystemMessage(systemMessageParam.getId());
        if(count>0){
            return Result.success("撤回系统消息成功");
        }else{
            return Result.error("撤回系统消息失败");
        }
    }
}
